<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Controller that handles all CMS Administrator Pages
 *
 * @author	kimon.web.designs - Application Framework
 * @package 	CMS
 * @subpackage 	Admin Class Controller
 * @category 	Framework
 * @copyright	Copyright (c) 2010, kimon.web.design
 */

class Admin extends Admin_Controller
{

    private $validation_rules = array();
    
    /**
     * Constructor method
     * @access public
     * @return void
     */
    public function  __construct() {
        parent::__construct();

        $this->load->model('page_model');
        $this->load->library('form_validation');

        $this->validation_rules = array(
                array(
                        'field' => 'title',
                        'label'	=> 'Title',
                        'rules'	=> 'trim|required|max_length[200]'
                ),
                array(
                        'field' => 'text',
                        'label'	=> 'text',
                        'rules' => 'trim|required'
                )
        );

        // Set the validation rules
        $this->form_validation->set_rules($this->validation_rules);
    }

    /**
     * Returns the Administrator Menu
     * @acess private
     * @return void
     */

    private function renderPageTable($page_list_data)
    {
        $data['data'] = $page_list_data;
        return $this->load->view('admin/page_list', $data, TRUE);
    }

    public function index()
    {
        $html = '';

        $page_list = $this->page_model->getAll();
        $html .= $this->renderPageTable($page_list);

        $this->template->write('content', $html);
        $this->template->render();
    }

    public function add()
    {
        $html = '<h1>Page Add</h1>';

        $this->template->write('content', $html);

        $this->template->render();
    }

    public function edit($id = 0)
    {
        if ($id==0) {
            redirect('admin/page');
        }

        $data['message'] = '';

        if ($this->input->post('action')=='update') {
            if ($this->form_validation->run())
            {
                $upddata['title'] = $this->input->post('title');
                $upddata['text'] = $this->input->post('text');
                $this->page_model->update($id, $upddata);
                redirect('admin/page');
            }
            else
            {
                $data['message'] = 'error';
            }

            $title = $this->form_validation->set_value('title');
            $text  = $this->form_validation->set_value('text');
        }
        else
        {
            $page  = $this->page_model->get($id);
            $title = $page->title;
            $text  = $page->text;
        }

        $data['id'] = $id;
        
        $data['title'] = array('name'    => 'title',
                               'id'      => 'title',
                               'type'    => 'text',
                               'value'   => $title
                              );

        $data['text'] = array('name'    => 'text',
                              'id'      => 'text',
                              'type'    => 'text',
                              'value'   => $text,
                              'class'   => 'tinymce'
                             );


        $html = $this->load->view('admin/page_form', $data, TRUE);

        $this->template->write('content', $html);
        $this->template->add_js('scripts/tiny_mce/jquery.tinymce.js');

        $this->load->helper('tinymce');
        $this->template->add_js(tinymce_script(), 'embed');
        $this->template->render();
    }


}

/* End of file pages.php */
/* Location: ./application/core/pages.php */